package com.work.tqyb.vm;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.work.tqyb.BuildConfig;
import com.work.tqyb.base.BaseViewModel;
import com.work.tqyb.http.response.CityListResponse;
import com.work.tqyb.http.response.QueryResponse;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class WeatherVM extends BaseViewModel {
    public ArrayList<CityListResponse.CityBean> cityList = new ArrayList<>();
    private MutableLiveData<ArrayList<CityListResponse.CityBean>> cityListLD = new MutableLiveData<>();
    private MutableLiveData<QueryResponse.ResultBean> queryLD = new MutableLiveData<>();
    public CityListResponse.CityBean choiceCity;


    public LiveData<ArrayList<CityListResponse.CityBean>> getCityListLD() {
        return cityListLD;
    }

    public LiveData<QueryResponse.ResultBean> getQueryLD() {
        return queryLD;
    }

    public void setChoiceCity(CityListResponse.CityBean choiceCity) {
        this.choiceCity = choiceCity;
    }


    public void getNetworkCityList() {
        HashMap<String, String> stringStringHashMap = new HashMap<>();
        stringStringHashMap.put("key", BuildConfig.AppKey);
        apiService
                .cityList(stringStringHashMap)
                .enqueue(new Callback<>() {
                    @Override
                    public void onResponse(Call<CityListResponse> call, Response<CityListResponse> response) {
                        if (response.body().result == null) {
                            ToastUtils.showShort(response.body().reason);
                            return;
                        }
                        cityList = response.body().result;
                        cityListLD.postValue(response.body().result);
                    }

                    @Override
                    public void onFailure(Call<CityListResponse> call, Throwable t) {

                    }
                });
    }

    public void getCityList(InputStream inputStream) {
        try {
            // 打开JSON文件
            // 创建一个字节数组来存储文件数据
            byte[] buffer = new byte[inputStream.available()];

            // 将文件数据读取到字节数组中
            inputStream.read(buffer);

            // 关闭输入流
            inputStream.close();

            // 将字节数组转换成字符串
            String json = new String(buffer, "UTF-8");

            // 现在可以使用json字符串进行操作
            CityListResponse response = GsonUtils.fromJson(json, CityListResponse.class);
            cityList = response.result;
            cityListLD.postValue(response.result);

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void searchList(String search) {
        ArrayList<CityListResponse.CityBean> list = new ArrayList<>();
        for (CityListResponse.CityBean city : cityList) {
            if (city.province.equals(search)) {
                list.add(city);
                continue;
            }
            if (city.city.equals(search)) {
                list.add(city);
                continue;
            }
            if (city.district.equals(search)) {
                list.add(city);
            }
        }
        cityListLD.postValue(list);
    }


    public void simpleWeatherQuery() {
        HashMap<String, String> stringStringHashMap = new HashMap<>();
        stringStringHashMap.put("key", BuildConfig.AppKey);
        stringStringHashMap.put("city", choiceCity.id);
        apiService
                .simpleWeatherQuery(stringStringHashMap)
                .enqueue(new Callback<>() {
                    @Override
                    public void onResponse(Call<QueryResponse> call, Response<QueryResponse> response) {
                        if (response.body().result == null) {
                            ToastUtils.showShort(response.body().reason);
                            return;
                        }
                        queryLD.postValue(response.body().result);
                    }

                    @Override
                    public void onFailure(Call<QueryResponse> call, Throwable t) {

                    }
                });
    }
}
